#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    Copyright (c) 2007, Jiří Procházka
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    The author can be reached via e-mail to Pro.J@seznam.cz
"""

import wx, os, sys, ConfigParser

def showerrwin(text, label='An error has occured:'):
    """show a wx dialog with text (error)"""
    class ErrDiagFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: ErrDiagFrame.__init__
            kwds['style'] = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN
            wx.Frame.__init__(self, *args, **kwds)
            self.errdiaglabel = wx.StaticText(self, -1, label)
            self.errdiagtextctrl = wx.TextCtrl(self, -1, text, style=wx.TE_MULTILINE)
            self.errdiagbutton = wx.Button(self, -1, 'Close')
            self.__set_properties()
            self.__do_layout()
            self.Bind(wx.EVT_BUTTON, self.jclosewin, self.errdiagbutton)
            # end wxGlade
        def __set_properties(self):
            # begin wxGlade: ErrDiagFrame.__set_properties
            self.SetTitle('Pyvcs')
            self.SetBackgroundColour(wx.NullColour)
            self.SetFocus()
            self.errdiaglabel.SetMinSize((172, 18))
            self.errdiaglabel.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ''))
            self.errdiagtextctrl.SetMinSize((400, 145))
            self.errdiagtextctrl.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ''))
            self.errdiagbutton.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ''))
            # end wxGlade
        def __do_layout(self):
            # begin wxGlade: ErrDiagFrame.__do_layout
            errdiagsizer = wx.BoxSizer(wx.VERTICAL)
            errdiagsizer.Add(self.errdiaglabel, 0, wx.LEFT|wx.TOP|wx.EXPAND|wx.ADJUST_MINSIZE, 4)
            errdiagsizer.Add(self.errdiagtextctrl, 0, wx.ALL|wx.ADJUST_MINSIZE, 4)
            errdiagsizer.Add(self.errdiagbutton, 0, wx.LEFT|wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 4)
            self.SetAutoLayout(True)
            self.SetSizer(errdiagsizer)
            errdiagsizer.Fit(self)
            errdiagsizer.SetSizeHints(self)
            self.Layout()
            # end wxGlade
        def jclosewin(self, event): # wxGlade: ErrDiagFrame.<event_handler>
            self.Destroy()
    # end of class ErrDiagFrame
    
    class ErrDiagW(wx.App):
        def OnInit(self):
            wx.InitAllImageHandlers()
            errdiagframe = ErrDiagFrame(None, -1, '')
            self.SetTopWindow(errdiagframe)
            errdiagframe.Show()
            return 1
    # end of class ErrDiagW
    errdiag = ErrDiagW(0)
    errdiag.MainLoop()
    
def genmenu(file):
    """function to view main menu"""
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: MyFrame.__init__
            kwds["style"] = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN
            wx.Frame.__init__(self, *args, **kwds)
            if file != False:
                self.button_1 = wx.Button(self, -1, "List versions")
                self.button_2 = wx.Button(self, -1, "List files in directory")
                self.button_3 = wx.Button(self, -1, "Extract version")
                self.button_4 = wx.Button(self, -1, "Add version")
                self.button_5 = wx.Button(self, -1, "Create archive")
                self.button_6 = wx.Button(self, -1, "Run diff application")
            self.button_9 = wx.Button(self, -1, "Configure Pyvcs")
            self.button_8 = wx.Button(self, -1, "Help")
            self.button_7 = wx.Button(self, -1, "Cancel")
            self.__set_properties()
            self.__do_layout()
            if file != False:
                self.Bind(wx.EVT_BUTTON, self.list, self.button_1)
                self.Bind(wx.EVT_BUTTON, self.listdir, self.button_2)
                self.Bind(wx.EVT_BUTTON, self.extract, self.button_3)
                self.Bind(wx.EVT_BUTTON, self.add, self.button_4)
                self.Bind(wx.EVT_BUTTON, self.create, self.button_5)
                self.Bind(wx.EVT_BUTTON, self.diff, self.button_6)
            self.Bind(wx.EVT_BUTTON, self.conf, self.button_9)
            self.Bind(wx.EVT_BUTTON, self.help, self.button_8)
            self.Bind(wx.EVT_BUTTON, self.cancel, self.button_7)
            # end wxGlade
            global hodn
            hodn = ''
        def __set_properties(self):
            # begin wxGlade: MyFrame.__set_properties
            self.SetTitle("Pyvcs")
            self.SetBackgroundColour(wx.NullColour)
            # end wxGlade
        def __do_layout(self):
            # begin wxGlade: MyFrame.__do_layout
            sizer_1 = wx.BoxSizer(wx.VERTICAL)
            if file != False:
                sizer_1.Add(self.button_2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
                sizer_1.Add(self.button_1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
                sizer_1.Add(self.button_3, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
                sizer_1.Add(self.button_4, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
                sizer_1.Add(self.button_5, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
                sizer_1.Add(self.button_6, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
            sizer_1.Add(self.button_9, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
            sizer_1.Add(self.button_8, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 4)
            sizer_1.Add(self.button_7, 0, wx.ALL|wx.ALIGN_BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 4)
            self.SetSizer(sizer_1)
            sizer_1.Fit(self)
            self.Layout()
            # end wxGlade
        def list(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'f'
            self.Destroy()
        def listdir(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'l'
            self.Destroy()
        def extract(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'e'
            self.Destroy()
            self.Destroy()
        def add(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'a'
            self.Destroy()
        def create(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'c'
            self.Destroy()
        def diff(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'd'
            self.Destroy()
        def conf(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'x'
            self.Destroy()
        def help(self, event): # wxGlade: MyFrame.<event_handler>
            global hodn
            hodn = 'h'
            self.Destroy()
        def cancel(self, event): # wxGlade: MyFrame.<event_handler>
            self.Destroy()

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    Pyvcs = MyFrame(None, -1, "")
    app.SetTopWindow(Pyvcs)
    Pyvcs.Show()
    app.MainLoop()
    return hodn
    
def prompt(question):
    """function for prompting user"""
    global hodn
    hodn = ''
    class PromptFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: PromptFrame.__init__
            kwds["style"] = wx.SYSTEM_MENU | wx.CAPTION | wx.CLIP_CHILDREN
            wx.Frame.__init__(self, *args, **kwds)
            self.label = wx.StaticText(self, -1, question)
            self.text = wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_ENTER)
            self.button = wx.Button(self, -1, "&OK")
            self.__set_properties()
            self.__do_layout()
            self.Bind(wx.EVT_BUTTON, self.OK, self.button)
            self.Bind(wx.EVT_TEXT_ENTER, self.OK, self.text)
            # end wxGlade
        def __set_properties(self):
            # begin wxGlade: PromptFrame.__set_properties
            self.SetTitle('Pyvcs')
            self.SetBackgroundColour(wx.NullColour)
            self.text.SetMinSize((250, 25))
            # end wxGlade
        def __do_layout(self):
            # begin wxGlade: PromptFrame.__do_layout
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
            sizer_2 = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.label, 0, wx.ALL|wx.EXPAND, 3)
            sizer_2.Add(self.text, 0, wx.EXPAND|wx.ALIGN_RIGHT, 0)
            sizer_1.Add(sizer_2, 1, wx.ALL|wx.EXPAND|wx.ALIGN_RIGHT, 3)
            sizer_1.Add(self.button, 0, wx.ALL|wx.ALIGN_RIGHT, 3)
            sizer.Add(sizer_1, 1, wx.EXPAND|wx.ALIGN_RIGHT, 0)
            self.SetSizer(sizer)
            sizer.Fit(self)
            self.Layout()
            # end wxGlade
        def OK(self, event): # wxGlade: PromptFrame.<event_handler>
            global hodn
            hodn = self.text.GetValue()
            self.Destroy()
    # end of class PromptFrame
    
    class PromptApp(wx.App):
        def OnInit(self):
            wx.InitAllImageHandlers()
            frame = PromptFrame(None, -1, "")
            self.SetTopWindow(frame)
            frame.Show()
            return 1
    # end of class PromptApp
    app = PromptApp(0)
    app.MainLoop()
    return hodn
    
def printinfo(info):
    """function printing information for user"""
    class PrintinfoFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: PrintinfoFrame.__init__
            kwds['style'] = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.RESIZE_BORDER
            wx.Frame.__init__(self, *args, **kwds)
            self.printinfotextctrl = wx.TextCtrl(self, -1, info, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP)
            self.printinfobutton = wx.Button(self, -1, 'Close')
            self.__set_properties()
            self.__do_layout()
            self.Bind(wx.EVT_BUTTON, self.jclosewin, self.printinfobutton)
            # end wxGlade
        def __set_properties(self):
            # begin wxGlade: PrintinfoFrame.__set_properties
            self.SetTitle('Pyvcs')
            self.SetBackgroundColour(wx.NullColour)
            self.SetFocus()
            self.printinfotextctrl.SetMinSize((450, 278))
            self.printinfotextctrl.SetFont(wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.NORMAL, wx.NORMAL, 0, ''))
            self.printinfobutton.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ''))
            # end wxGlade
        def __do_layout(self):
            # begin wxGlade: PrintinfoFrame.__do_layout
            printinfosizer = wx.BoxSizer(wx.VERTICAL)
            printinfosizer.Add(self.printinfotextctrl, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 4)
            printinfosizer.Add(self.printinfobutton, 0, wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 4)
            self.SetAutoLayout(True)
            self.SetSizer(printinfosizer)
            printinfosizer.Fit(self)
            printinfosizer.SetSizeHints(self)
            self.Layout()
            # end wxGlade
        def jclosewin(self, event): # wxGlade: PrintinfoFrame.<event_handler>
            self.Destroy()
    # end of class PrintinfoFrame
    
    class PrintinfoW(wx.App):
        def OnInit(self):
            wx.InitAllImageHandlers()
            printinfoframe = PrintinfoFrame(None, -1, '')
            self.SetTopWindow(printinfoframe)
            printinfoframe.Show()
            return 1
    # end of class PrintinfoW
    printinfo = PrintinfoW(0)
    printinfo.MainLoop()

def filedialog(text, open = 1):
    """file save/open dialog; open!=1 means for save dialog"""
    class FiledialogApp(wx.App):
        def OnInit(self):
            wx.InitAllImageHandlers()
            if open == 1:
                dialog = wx.FileDialog(None, text, style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
            else:
                dialog = wx.FileDialog(None, text, style = wx.FD_SAVE | wx.OVERWRITE_PROMPT)
            self.SetTopWindow(dialog)
            global hodn2
            if dialog.ShowModal() == wx.ID_OK:
                hodn2 = dialog.GetPath()
            else:
                hodn2 = ''
            return 1
    app = FiledialogApp(0)
    app.MainLoop()
    return hodn2
    
def conf():
    """configuration window"""
    #reading configuration
    conffile = open(os.path.join(os.path.dirname(sys.argv[0]),'pyvcs.conf'), 'r')        #open pyvcs file and read to ConfigParser from it
    conf = ConfigParser.ConfigParser()
    conf.readfp(conffile)
    conffile.close()
    class ConfFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: MyFrame1.__init__
            kwds["style"] = wx.SYSTEM_MENU | wx.CAPTION | wx.CLIP_CHILDREN
            wx.Frame.__init__(self, *args, **kwds)
            self.label_2 = wx.StaticText(self, -1, "Use graphical user interface (GUI)")
            self.checkbox_1 = wx.CheckBox(self, -1)
            self.label_3 = wx.StaticText(self, -1, "Path to a diff application")
            self.text_ctrl_3 = wx.TextCtrl(self, -1, '')
            self.panel_2 = wx.Panel(self, -1)
            self.button_1 = wx.Button(self, -1, "OK")
            self.__set_properties()
            self.__do_layout()
            self.Bind(wx.EVT_BUTTON, self.dsda, self.button_1)
            # end wxGlade
        def __set_properties(self):
            # begin wxGlade: MyFrame1.__set_properties
            self.SetTitle("Pyvcs Configuration")
            self.SetBackgroundColour(wx.NullColour)
            self.checkbox_1.SetValue(int(conf.get('Main', 'usegui')))
            self.text_ctrl_3.SetValue(str(conf.get('Main', 'diffapp')))
            # end wxGlade
        def __do_layout(self):
            # begin wxGlade: MyFrame1.__do_layout
            grid_sizer_1 = wx.GridSizer(3, 2, 1, 1)
            grid_sizer_1.Add(self.label_2, 0, wx.LEFT|wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 4)
            grid_sizer_1.Add(self.checkbox_1, 0, wx.BOTTOM|wx.TOP|wx.ALIGN_CENTER_VERTICAL, 4)
            grid_sizer_1.Add(self.label_3, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
            grid_sizer_1.Add(self.text_ctrl_3, 0, wx.BOTTOM|wx.RIGHT|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 4)
            grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
            grid_sizer_1.Add(self.button_1, 0, wx.BOTTOM|wx.ALIGN_BOTTOM, 4)
            self.SetSizer(grid_sizer_1)
            grid_sizer_1.Fit(self)
            self.Layout()
            # end wxGlade
        def dsda(self, event): # wxGlade: MyFrame1.<event_handler>
            global usegui, diffapp
            usegui = int(self.checkbox_1.GetValue())
            diffapp = str(self.text_ctrl_3.GetValue())
            self.Destroy()
    # end of class MyFrame1
    
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = ConfFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()
    return {'usegui': usegui, 'diffapp': diffapp}
